package com.exception.constructor;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;

public class InputFile {

	private BufferedReader in;

	public InputFile(String filename) throws Exception {
		try {
			in = new BufferedReader(new FileReader(filename));
		} catch (FileNotFoundException e) {
			System.out.println("不能打开文件：" + filename);
			throw e;
		} catch (Exception e) {
			// 其它所有异常都需要关闭in
			dispose();
		} finally {
			// 不能在这里关闭in，因为这样每次创建完对象后就是关闭in，我们其实是希望在使用完对象后，再关闭in
		}
	}

	public String getLine() {
		String s;
		try {
			s = in.readLine();
		} catch (IOException e) {
			// 抛出一个编程错误，这种异常是无须捕获的
			throw new RuntimeException();
		}
		return s;
	}

	public void dispose() {
		try {
			in.close();
		} catch (IOException e) {
			throw new RuntimeException("关闭失败");
		}
	}

}
